import CANNON from "cannon";
import RescacheMgr from "../../framework/inc/rescachemgr";
import StagerMgr from "../../framework/inc/stagemgr";
import { LV_CONFIG, OBSTACLE_LINE_CONFIG, OBSTACLE_CELL_CONFIG } from "./lvconfig";
import { DEBUG } from "../../relconf";

export default class TestWorld {
    constructor(scene, camera) {
        this._scene = scene;
        this._camera = camera;

        this._g_statu = WORLD_STATU['IDLE'];
        this._player = undefined;
        this._dir_speed = 0;

        this._world = this._init_phy();
    }

    static getWorld(scene, camera) {
        if (!this._inc && scene && camera) {
            this._inc = new PhyWorld(scene, camera);
        }
        return this._inc;
    }

    _init_env() {
        //fog
        let fog = new THREE.Fog(0xffffff, 1, 100);
        this._scene.fog = fog;
        //ground 
        let ground = d.ground("res/texture/ground.png", 1024, 1024)
        this._scene.add(ground);
        //skybox 
        const tplist = [
            "purplenebula_lf.png", // left
            "purplenebula_rt.png", // right
            "purplenebula_up.png", // top
            "purplenebula_dn.png", // bottom
            "purplenebula_bk.png", // back
            "purplenebula_ft.png"  // front
        ];
        let skybox = d.skybox(tplist);
        this._scene.background = skybox;
    }

    _init_phy() {
        //world
        let world = new CANNON.World();
        world.gravity.set(0, 10, 0);
        world.broadphase = new CANNON.NaiveBroadphase();
        this._world = world;
        this._crt_baseenv();

        this._res_mgr = RescacheMgr.getInstance();

        this._init_env();
        this._init_landpool(1);
        this._add_player(new CANNON.Vec3(0, 0, -1))
        this.startRunning();

        //show start 
        // StagerMgr.getInstance().showView("viewstart");
        return world;
    }

    _init_player() {

    }

    get world() {
        return this._world;
    }

    reLoad() {
        this._g_statu = WORLD_STATU["IDLE"];
    }

    startRunning() {
        this._g_statu = WORLD_STATU["RUNNING"];
    }

    _init_landpool(lvnum) {
        if (!this._obs_mgr) {
            this._obs_mgr = new ObstacleMgr(this._world, this._res_mgr);
        }
        let landobj;
        for (let i = 0; i < VIEW_LANDCNT; i++) {
            landobj = this._obs_mgr.newLand(lvnum);
            landobj.position.z = - landobj.l_length * i;
            this._scene.add(landobj);
        }
    }

    _crt_baseenv() {
        let ground_shape = new CANNON.Plane();
        let ground_body = new CANNON.Body({
            mass: 0,
            shape: ground_shape
        });
        ground_body.quaternion.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), -Math.PI / 180 * 90)
        this._world.add(ground_body);
    }

    _add_player(vec3) {
        let sphere_shape = new CANNON.Box(new CANNON.Vec3(0.5, 1, 0.5));
        let sphere_body = new CANNON.Body({
            mass: 5,
            position: vec3,
            shape: sphere_shape,
            type: CANNON.Body.DYNAMIC
        });
        this._world.add(sphere_body);

        let mesh_box = this._res_mgr.cloneGLTF("box");
        this._scene.add(mesh_box);
        sphere_body.addEventListener("collide", (e) => {
            // this.reLoad();
        });
        RigidMeshMgr.getInstance().bind(mesh_box, sphere_body);
        this._player = mesh_box;
    }

    _async_phypos() {

    }

    _chk_movedirec(x, y) {
        if (!x && !y) this._dir_speed = 0;
        if (x > d.w / 2) {
            this._dir_speed = 1;
        }
        else {
            this._dir_speed = -1;
        }
    }

    onTouchStart(params) {
        let evt = params[0];
        let camera = params[1]
        this._scene.chkTouchGUI(evt, camera);

        touch_sx = evt.touches[0].pageX, touch_sy = evt.touches[0].pageY;
        this._chk_movedirec(touch_sx, touch_sy);
    }

    onTouchMove(params) {
        let evt = params[0];
        let camera = params[1]
        mv_nx = evt.targetTouches[0].pageX, mv_ny = evt.targetTouches[0].pageY;
        this._chk_movedirec(mv_nx, mv_ny);

        /* 
                检查滑动方向-
        
                mv_disx = mv_nx - touch_sx, mv_disy = mv_ny - touch_sy;
                if (Math.abs(mv_disx) > Math.abs(mv_disy)) {
                    if (mv_disx > 0) {
                        console.log("move right");
                        camera.position.x += 0.1;
                        if (this._player) {
                            this._player.position.x += 0.1;
                        }
                    }
                    else {
                        console.log("move left");
                        camera.position.x -= 0.1;
                        if (this._player) {
                            this._player.position.x -= 0.1;
                        }
                    }
                }
                else {
                    if (mv_disy > 0) {
                        console.log('move down');
                    }
                    else {
                        console.log("move up");
                    }
                } 
                
                */
    }

    onTouchEnd(params) {
        let evt = params[0];
        this._dir_speed = 0;
    }

    update() {
        //running man 
        if (this._player) {
            this._player.position.x += this._dir_speed * RUNNING_SPEED;
            this._camera.position.x = this._player.position.x;
        }
        RigidMeshMgr.getInstance().update();
        if (this._g_statu == WORLD_STATU["RUNNING"]) {
            this._obs_mgr.scrollLand();
        }
    }
}